﻿using RimWorld;
using RimWorld.Planet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;

namespace SZ.Mech
{
    public class TransportPodsArrivalAction_VerbLandInSpecificCell : TransportPodsArrivalAction
    {
        private MapParent mapParent;
        private IntVec3 cell;
        private ThingDef activeDropPodDef;
        private ThingDef dropPodIncomingDef;

        public TransportPodsArrivalAction_VerbLandInSpecificCell(MapParent mapParent, IntVec3 cell, ThingDef activeDropPodDef, ThingDef dropPodIncomingDef)
        {
            this.mapParent = mapParent;
            this.cell = cell;
            this.activeDropPodDef = activeDropPodDef;
            this.dropPodIncomingDef = dropPodIncomingDef;
        }

        public override FloatMenuAcceptanceReport StillValid(IEnumerable<IThingHolder> pods, int destinationTile)
        {
            FloatMenuAcceptanceReport floatMenuAcceptanceReport = base.StillValid(pods, destinationTile);
            if (!floatMenuAcceptanceReport)
            {
                return floatMenuAcceptanceReport;
            }
            if (mapParent != null && mapParent.Tile != destinationTile)
            {
                return false;
            }
            return CanLandInSpecificCell(pods, mapParent);
        }
        public static bool CanLandInSpecificCell(IEnumerable<IThingHolder> pods, MapParent mapParent)
        {
            if (mapParent == null || !mapParent.Spawned || !mapParent.HasMap)
            {
                return false;
            }
            if (mapParent.EnterCooldownBlocksEntering())
            {
                //消息进入冷却块进入
                return FloatMenuAcceptanceReport.WithFailMessage("MessageEnterCooldownBlocksEntering".Translate(mapParent.EnterCooldownTicksLeft().ToStringTicksToPeriod()));
            }
            return true;
        }
        public override void Arrived(List<ActiveDropPodInfo> pods, int tile)
        {
            Thing lookTarget = TransportPodsArrivalActionUtility.GetLookTarget(pods);

            MiaoUtility.DropTravelingTransportPods(pods, cell, mapParent.Map, activeDropPodDef, dropPodIncomingDef);
            //TransportPodsArrivalActionUtility.DropTravelingTransportPods(pods, cell, mapParent.Map);
            Messages.Message("单位降落".Translate(), lookTarget, MessageTypeDefOf.TaskCompletion);
        }

        public override void ExposeData()
        {
            base.ExposeData();
            Scribe_References.Look(ref mapParent, "mapParent");
            Scribe_Values.Look(ref cell, "cell");

            Scribe_Defs.Look(ref activeDropPodDef, "activeDropPodDef");
            Scribe_Defs.Look(ref dropPodIncomingDef, "dropPodIncomingDef");
        }
    }
}
